﻿#include "analyzer.h"

Analyzer::Analyzer(){}

Analyzer::Analyzer(const QString &filePath) :
    _filePath(filePath){}

bool Analyzer::openFile(const QString &filePath)
{
    if(!QFile::exists(filePath))
        return false;

    return capture.open(filePath.toStdString());
}

bool Analyzer::open()
{
    if(!QFile::exists(_filePath))
        return false;

    return capture.open(_filePath.toStdString());
}

int Analyzer::getFrameCount() const
{
    double value=capture.get(CAP_PROP_FRAME_COUNT);
    return qRound(value);
}

int Analyzer::getFrameHeight() const
{
    double value=capture.get(CAP_PROP_FRAME_HEIGHT);
    return qRound(value);
}

int Analyzer::getFrameWidth() const
{
    double value=capture.get(CAP_PROP_FRAME_WIDTH);
    return qRound(value);
}

int Analyzer::getFrameRate() const
{
    double value=capture.get(CAP_PROP_FPS);
    return qRound(value);
}

void Analyzer::printVideoInfo()
{
    qDebug() << "Video Information:" << "\n"
             << "Frame Count: " << getFrameCount() << "\n"
             << "Frame Width: " << getFrameWidth() << "\n"
             << "Frame Height: " << getFrameHeight() << "\n"
             << "Frame Rate: " << getFrameRate() << "\n";
}

void Analyzer::writeVideoInfo()
{
    QFileInfo fileInfo(_filePath);
    QString fileName=fileInfo.baseName();
    QString path=fileInfo.path();
    QString filePath=path + "/" + fileName + ".json";
    QFile file(filePath);
    if(file.open(QFile::WriteOnly | QFile::Text))
    {
        QJsonObject obj;
        obj.insert("frameCount",getFrameCount());
        obj.insert("frameWidth",getFrameWidth());
        obj.insert("frameHeight",getFrameHeight());
        obj.insert("frameRate",getFrameRate());

        QJsonDocument doc(obj);
        file.write(doc.toJson());

        file.close();
    }
}
